/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package motiveai.tests;

import com.jme3.font.BitmapText;
import com.jme3.scene.Spatial;
import motiveai.annotation.DurationOfBehaviour;
import motiveai.components.BehaviourEntity;
import motiveai.factors.Environment;

/**
 *
 * @author oxplay
 */
public class Test2AttackEnemy extends BehaviourEntity<Test2Agent> {

    public Test2AttackEnemy() {
    }

    public boolean canBeUsed(Test2Agent agent, Environment environment) {
        if (!environment.getBooleanFactor("isEnemyClose")) {
            return false;
        }
        return true;
    }

    @Override
    @DurationOfBehaviour(duration = 0.3f)
    public float behave(Test2Agent agent, Environment environment) {
        Spatial spat = agent.getSpatial();
        BitmapText text = spat.getUserData("text");
        text.setText("Attack enemy!");
        if (environment.getBooleanFactor("isEnemyClose")) {
            environment.setBooleanFactor("isAttacking", true);
            environment.setBooleanFactor("isRested", false);
            float distance = agent.getSpatial().getLocalTranslation().distance(agent.closeEnemy.getLocalTranslation());
            if (distance < 5f) {
                agent.getSpatial().getControl(MoveControl.class).moveTo(agent.getSpatial().getLocalTranslation());
                int health = agent.closeEnemy.<Integer>getUserData("health");
                health -= 10;
                agent.closeEnemy.setUserData("health", health);
                if (health <= 0) {
                    agent.closeEnemy.removeFromParent();
                    agent.closeEnemy = null;
                }
            } else {
                agent.getSpatial().getControl(MoveControl.class).moveTo(agent.closeEnemy.getLocalTranslation());
            }
        }
        return 0;
    }
}
